﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using Ext.Net;
using Ext.Net.MVC;
using CustomMVC;
using System.Drawing;
using System.IO;
using CommonLib;
using EFMysql;
using BLL;
using System.Diagnostics;
using System.Collections;

namespace ShuaDanMysql.Controllers
{
    [DirectController]
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        UserBll userBll = new UserBll();

        public ActionResult Index()
        {

            return View();
        }

        public ActionResult Regist()
        {
            return View();
        }

        [ErrorOper]
        public ActionResult Captcha()
        {
            ADSSAntiBot captcha = new ADSSAntiBot();
            string str = captcha.DrawNumbers(5);
            if (Session[ADSSAntiBot.SESSION_CAPTCHA] == null)
            {
                Session.Add(ADSSAntiBot.SESSION_CAPTCHA, str);
            }
            else
            {
                Session[ADSSAntiBot.SESSION_CAPTCHA] = str;
            }
            Bitmap bmp = captcha.Result;
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return File(ms.ToArray(), "image/jpeg");
        }

        [DirectMethod]
        [HttpPost, OpLog("用户登录"), ErrorOper]
        public ActionResult Login(string loginName, string loginPassword, string captcha)
        {
            if (Session["CAPTCHA"].ToString() == captcha)
            {
                Trace.WriteLine(DateTime.Now + "用户名：" + loginName + "尝试登陆" + "\n\r");
                pre_user user = new pre_user
                {
                    username = loginName,
                    password = GeneralTools.MD5Encrpyt(ConfigurationManager.AppSettings["passsalt"].ToString() + loginPassword),
                    logip = Request.UserHostAddress
                };

                Tuple<pre_user, int, List<pre_privileges>> list = userBll.Login(user);
                if (!Equals(list, null))
                {
                    if (list.Item1.islive.Value)
                    {
                        List<Tuple<pre_user, string, int, List<pre_privileges>>> loginUser = WebVar<Tuple<pre_user, string, int, List<pre_privileges>>>.GetList;
                        loginUser.Add(new Tuple<pre_user, string, int, List<pre_privileges>>(list.Item1, Session.SessionID, list.Item2, list.Item3));
                        Trace.WriteLine(DateTime.Now + "用户名：" + loginName + "登陆成功" + "\n\r");

                        return this.Direct(new CustomMVC.RequestResult { Result = "OK", Message = "/Main/" });
                    }else
                    {
                        Trace.WriteLine(DateTime.Now + "用户名：" + loginName + "尚未激活" + "\n\r");

                        return this.Direct(new CustomMVC.RequestResult { Result = "Fail", Message = "用户：" + loginName + "尚未激活" });
                    }
                }
                else
                {
                    Trace.WriteLine(DateTime.Now + "用户名：" + loginName + "登陆失败，用户名或密码不正确！");
                    return this.Direct(new CustomMVC.RequestResult { Result = "Fail", Message = "用户名或密码不正确！" });
                }
            }
            else
            {
                Trace.WriteLine("用户名：" + loginName + "登陆失败，验证码不正确！");
                return this.Direct(new CustomMVC.RequestResult { Result = "Fail", Message = "验证码不正确！" });
            }

        }

        [DirectMethod, HttpPost, OpLog("用户注册"), ErrorOper]
        public ActionResult Regist(string regName, string regPassword, string repeatloginPassword,string shopId, string telNo, string eMail, string Qq, string
          captcha)
        {
            Hashtable ht = new Hashtable();
            if (Session["CAPTCHA"].ToString() == captcha)
            {
                if (Equals(regPassword, repeatloginPassword))
                {
                    pre_user newUser = new pre_user
                    {
                        username = regName,
                        password = GeneralTools.MD5Encrpyt(ConfigurationManager.AppSettings["passsalt"].ToString() + regPassword),
                        balance = 0,
                        createtime = GeneralTools.ConvertDateTimeInt(DateTime.Now),
                        islive = false,
                        name = regName,
                        tel = telNo,
                        role = "user",
                        createip = Request.UserHostAddress,
                        qq = Qq,
                        logtime = 0,
                        logcount=0,
                        email = eMail,
                        shopid = shopId,
                        createid = -1,
                        updateid = -1,
                        discount = 10,
                        ordercount=0,
                        belongto=0,
                        GoldCoinAmount = 0,
                        updatetime = GeneralTools.ConvertDateTimeInt(DateTime.Now),

                    };
                    if (userBll.UserReg(newUser))
                    {
                        ht.Add("Result", "OK");
                        ht.Add("Message", "/Home/");

                    }
                }
                else
                {
                    ht.Add("Result", "FAIL");
                    ht.Add("Message", "密码不一致，请检查");
                }
            }else
            {
                ht.Add("Result", "FAIL");
                ht.Add("Message", "注册失败，验证码不正确！");
            }
            return this.Direct(ht);
        }

    }
}